/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avenue;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.AppUser;
import model.DBManager;
import model.Vehicle;

/**
 *
 * @author apostolis ΕΔΩ ΘΑ ΒΑΛΟΥΜΕ ΓΕΝΙΚΕΣ CLASSES
 */
public class Tools {

    public static String appName = "Tolls manager";
    public static String appVersion = "ver 1.0";
    private static String dbURL = "jdbc:derby://localhost:1527/avenue;user=avenue;password=123";
    private static Connection conn;
    public static String msgSystemProtected = "Εγγραφή προτατευόμενη από το σύστημα!";
    public static String msgCantDeleteOnlineUser = "Δεν μπορείτε να διαγράψετε χρήστη που είναι online!";
    public static String msgUserExists = "Ο χρήστης %s υπάρχει. Παρακαλώ διορθώστε.;";
    public static String msgVehicleExists = "Το όχημα %s υπάρχει. Παρακαλώ διορθώστε.;";
        
    public static String appTitle() {
        return appName + " " + appVersion;
    }

    public static void createConnection() {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
            //Get a connection
            conn = DriverManager.getConnection(dbURL);
        } catch (Exception except) {
            System.out.println("ERROR: " + except.getMessage());
        }
    }

    public static void shutdown() {
        try {
            if (conn != null) {
                DriverManager.getConnection(dbURL + ";shutdown=true");
                conn.close();
            }
        } catch (SQLException sqlExcept) {
        }
    }

    public static void centerOnScreen(JFrame f) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();

        //Calculate the frame location  
        int x = (screenSize.width - f.getWidth()) / 2;
        int y = (screenSize.height - f.getHeight()) / 2;

        //Set the new frame location  
        f.setLocation(x, y);
    }

    public static void centerOnScreen(JDialog f) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();

        //Calculate the frame location  
        int x = (screenSize.width - f.getWidth()) / 2;
        int y = (screenSize.height - f.getHeight()) / 2;

        //Set the new frame location  
        f.setLocation(x, y);
    }

    //Get the screen size  
    public static void centerOnParent(JDialog f) {
        int x;
        int y;

        // Find out our parent 
        Container myParent = f.getParent();
        Point topLeft = myParent.getLocationOnScreen();
        Dimension parentSize = myParent.getSize();

        Dimension mySize = f.getSize();

        if (parentSize.width > mySize.width) {
            x = ((parentSize.width - mySize.width) / 2) + topLeft.x;
        } else {
            x = topLeft.x;
        }

        if (parentSize.height > mySize.height) {
            y = ((parentSize.height - mySize.height) / 2) + topLeft.y;
        } else {
            y = topLeft.y;
        }

        f.setLocation(x, y);
        f.setVisible(true);
        f.requestFocus();
    }

    public static AppUser appUserByUserName(String userName) {
        try {
            return DBManager.em.createNamedQuery("AppUser.findByUserName",
                    AppUser.class).setParameter("userName", userName).getSingleResult();
        } catch (Exception ex) {
            return null;
        }
    }

    public static Vehicle VehicleByRegNumber(String regNumber) {
        try {
            return DBManager.em.createNamedQuery("Vehicle.findByRegNumber",
                    Vehicle.class).setParameter("regNumber", regNumber).getSingleResult();
        } catch (Exception ex) {
            return null;
        }
    }
}
